<!DOCTYPE html>
<meta charset=utf-8>
<title>Subresource Integrity loading the same resource many times in a row, with different integrity values</title>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>

<div id="log"></div>

<div id="container"></div>
<script>
    var SRIScriptTest = function(pass, name, src, integrityValue, crossoriginValue) {
        this.pass = pass;
        this.name = "Script: " + name;
        this.src = src;
        this.integrityValue = integrityValue;
        this.crossoriginValue = crossoriginValue;
    }

    SRIScriptTest.prototype.execute = function() {
        var test = async_test(this.name);
        var e = document.createElement("script");
        e.src = this.src;
        e.setAttribute("integrity", this.integrityValue);
        if(this.crossoriginValue) {
            e.setAttribute("crossorigin", this.crossoriginValue);
        }
        if(this.pass) {
            e.addEventListener("load", function() {test.done()});
            e.addEventListener("error", function() {
                test.step(function(){ assert_unreached("Good load fired error handler.") })
            });
        } else {
           e.addEventListener("load", function() {
                test.step(function() { assert_unreached("Bad load succeeded.") })
            });
           e.addEventListener("error", function() {test.done()});
        }
        document.body.appendChild(e);
    };

    window.result = false;

    new SRIScriptTest(
        true,
        "Same-origin with correct sha256 hash.",
        "resources/simple-result.js",
        "sha256-xJjeC55VpJ4nhSNBPBfw2QrsylPyXeQGxMd1l4unH58="
    ).execute();

    new SRIScriptTest(
        true,
        "Same-origin with correct sha384 hash.",
        "resources/simple-result.js",
        "sha384-VsVEqbvsJzugHbbmJwqnn6S5D8oMWupN9NRvS7irxl3+YHlxEjKC6q5nWWC7dlmP"
    ).execute();

    new SRIScriptTest(
        true,
        "Same-origin with correct sha512 hash.",
        "resources/simple-result.js",
        "sha512-9Zotk1Xb/bjPvI/B9vAB3osGwSxgL4vnadYWg6uHLDFnO2pV71DWCUxJrdBLNqwxPcS3EieDnKCzQ01kwE+LNw=="
    ).execute();

    new SRIScriptTest(
        true,
        "Same-origin with empty integrity.",
        "resources/simple-result.js",
        ""
    ).execute();

    new SRIScriptTest(
        false,
        "Same-origin with incorrect hash.",
        "non-resources/simple-result.js",
        "sha256-deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdead"
    ).execute();

    new SRIScriptTest(
        true,
        "Same-origin with multiple sha256 hashes, including correct.",
        "resources/simple-result.js",
        "sha256-xJjeC55VpJ4nhSNBPBfw2QrsylPyXeQGxMd1l4unH58= sha256-deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdead"
    ).execute();

    new SRIScriptTest(
        true,
        "Same-origin with sha256 mismatch, sha512 match",
        "resources/simple-result.js",
        "sha512-9Zotk1Xb/bjPvI/B9vAB3osGwSxgL4vnadYWg6uHLDFnO2pV71DWCUxJrdBLNqwxPcS3EieDnKCzQ01kwE+LNw== sha256-deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdead"
    ).execute();

    new SRIScriptTest(
        false,
        "Same-origin with sha256 match, sha512 mismatch",
        "resources/simple-result.js",
        "sha512-deadbeefspbnUnwooKGNNCb39nvg+EW0O9hDScTXeo/9pVZztLSUYU3LNV6H0lZapo8bCJUpyPPLAzE9fDzpxg== sha256-xJjeC55VpJ4nhSNBPBfw2QrsylPyXeQGxMd1l4unH58= sha256-deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdead"
    ).execute();
</script>
